//== SAGITTARIUS ============================================================================

//== BEGINNING OF CODE ===============================================================

#ifndef __SAGITTARIUS__GMPUNICODE__H_
#define __SAGITTARIUS__GMPUNICODE__H_

//== INCLUDES ========================================================================

#include "sagittarius/core/SagittariusCore.h"
#include "sagittarius/gmp/gmpsupport.h"

#include <stdio.h>
#include <gmp.h>

//Constants
/** @brief The value assumed by the null character (should be zero) */
#define UC_Null 0

#ifdef __cplusplus
extern "C" {
#endif

///Type to hold Unicode codepoint (must be at least as big as largest allowed codepoint)
typedef bignum_t GMPUnicodeChar;

/** @fn  UTF8_GMPConvertBuffer
 *  @return the total number of converted characters
    Converts null-terminated UTF8 buffer into Unicode codepoint representation.  
    The out buffer must be at least 4x the size of the input buffer in bytes (including the null character -
    output buffer is also null-terminated).
**/
int UTF8_GMPConvertBuffer(const char* in, GMPUnicodeChar* out);

/** @brief Encodes the buffer into a bytestream
 * @details Call @ref UCGMP_UTF8BufferLen to get the required length
 * of the char array
 */
int UCGMPEncode_UTF8(GMPUnicodeChar* buf, char* out);

//get n bytes
int UCGMPgetNBytes(GMPUnicodeChar chr);

/**
 * @fn  UCGMPShrinkBuffer
 * @return The new buffer pointer
 * Shrinks the buffer to the size specified.
 * This is useful, for example, when the buffer is preemtively
 * allocated to a larger size than ultimately necessary.
 * Because the number of DWORDS (i.e. the true number of 
 * characters in the bitstream) cannot be known ahead of
 * time, the buffer must be allocated to 4x the size of
 * the input buffer. If the input stream contains multi-byte
 * characters, this will be an over-estimate.
 * NOTE: old buffer is freed
   **/
GMPUnicodeChar* UCGMPShrinkBuffer(GMPUnicodeChar* buf, size_t new_size);

///Get length of hypothetical ENCODED buffer including null-terminating char
size_t UCGMP_UTF8BufferLen(GMPUnicodeChar* buf);

///Non-encoded
size_t UCGMPBufferLen(GMPUnicodeChar* buf);

#ifdef __cplusplus
}//extern "C"
#endif

#endif
